﻿'Copyright (C) 2014  Ali Nasahth

'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program.  If not, see <http://www.gnu.org/licenses/>


Imports System.IO
Imports System.ComponentModel
Imports iTextSharp.text.pdf

Public Class Form1

    Private WithEvents bw As BackgroundWorker = New BackgroundWorker

    Dim inf, out, org As String
    Dim SfileCount As Integer = 0
    Dim Spiltted As Integer = 0
    Dim Moved As Integer = 0

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtWatch.Text = FolderBrowserDialog1.SelectedPath
            inf = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtOut.Text = FolderBrowserDialog1.SelectedPath
            out = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub ButtonSplit_Click(sender As Object, e As EventArgs) Handles ButtonSplit.Click
        Timer1.Start()
        If Not bw.IsBusy Then
            bw.RunWorkerAsync()
        End If
        btnCancel.Enabled = True
        ButtonSplit.Enabled = False
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            textORginal.Text = FolderBrowserDialog1.SelectedPath
            org = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        bw.WorkerReportsProgress = True
        bw.WorkerSupportsCancellation = True
    End Sub

    Private Sub bw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bw.DoWork
        Dim dr As DirectoryInfo = New DirectoryInfo(inf)
        Dim filelist() As FileInfo = dr.GetFiles("*.*", SearchOption.TopDirectoryOnly)
        For Each f In filelist
            If Not FileInUse(f.FullName) Then

                If GETPDFPageCount(f.FullName) > 1 Then
                    Dim st As String = """" & f.FullName & """ burst " & "output """ & out & "\" & f.Name.Replace(".pdf", "") & "_page_%02d.pdf"""
                    Dim pi As Process = Process.Start("pdftk.exe", st)
                    pi.WaitForExit()

                    Try
                        System.IO.File.Move(f.FullName, org & "\" & SfileCount & f.Name)
                    Catch ex As Exception

                    End Try
                    Spiltted += 1
                Else
                    Try
                        System.IO.File.Copy(f.FullName, out & "\" & Moved & f.Name)
                        System.IO.File.Move(f.FullName, org & "\" & SfileCount & f.Name)
                    Catch ex As Exception

                    End Try      
                    Moved += 1
                End If
                SfileCount += 1
            End If

        Next
    End Sub


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Not bw.IsBusy Then
            bw.RunWorkerAsync()
        End If
        lblScanned.Text = SfileCount
        lblSplitted.Text = Spiltted
        lblSInglePage.Text = Moved
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        bw.CancelAsync()
        Timer1.Stop()
        ButtonSplit.Enabled = True
        btnCancel.Enabled = False
    End Sub

    Public Function FileInUse(ByVal sFile As String) As Boolean
        Dim thisFileInUse As Boolean = False
        If System.IO.File.Exists(sFile) Then
            Try
                Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    ' thisFileInUse = False
                End Using
            Catch
                thisFileInUse = True
            End Try
        End If
        Return thisFileInUse
    End Function

    Public Function GETPDFPageCount(ByVal filepath As String) As Integer
        Dim page_count As Integer = 0
        Try
            'check for the extension
            Dim extension As String = Path.GetExtension(filepath)
            If extension = ".PDF" OrElse extension = ".pdf" Then
                'Create instance for the PDF reader
                Dim pdf_fie As New PdfReader(filepath)
                'read it's pagecount
                page_count = pdf_fie.NumberOfPages
                'close the file
                pdf_fie.Close()
            End If
        Catch ex As PdfException
            '
        End Try
        Return page_count
    End Function

  
End Class




